package com.choosefine.it.manager.controller;

import java.util.HashMap;
import java.util.Map;

import com.choosefine.it.common.def.CommonConstant;
import com.choosefine.it.team.model.TeamFindProjectInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import com.choosefine.it.base.BaseController;
import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.common.def.ResultCode;
import com.choosefine.it.common.model.CustomRestTemplate;
import com.choosefine.it.common.utils.DateUtils;
import com.choosefine.it.common.utils.IntegerUtil;
import com.choosefine.it.common.utils.StringUtils;
import com.choosefine.it.common.utils.URLHandlerUtils;
import com.choosefine.it.manager.model.ManagerFindTeamInfo;
import com.choosefine.it.manager.model.ManagerTeamCollection;
import com.choosefine.it.manager.model.TeamContractorInfo;
import com.choosefine.it.manager.service.ManagerFindTeamInfoService;
import com.choosefine.it.manager.service.ManagerTeamCollectionService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * @author 潘钱钦 下午3:08:38
 * 
 */
@RestController
@RequestMapping("/managerFindTeam")
@Api(value = "项目经理找班组", description = "俞鑫")
public class ManagerFindTeamController extends BaseController {
	@Autowired
	private ManagerFindTeamInfoService managerFindTeamInfoService;
	@Autowired
	private ManagerTeamCollectionService managerTeamCollectionService;
	@Autowired
	CustomRestTemplate restTemplate;
	@Autowired
	ObjectMapper jsonObjectMapper;
	Logger logger = LoggerFactory.getLogger(ManagerFindTeamController.class);

	/*yuxin3.6*/
	@ApiOperation(value = "项目经理根据条件找班组和搜索", response = ManagerFindTeamInfo.class)/*yuxin3.3*/
	@ApiImplicitParams({
			@ApiImplicitParam(name = "teamType", value = "班组类型的id", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "areaCode", value = "班组期望的工作地区", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "random", value = "班组类别名称/班组承包人姓名", required = false, dataType = "String", paramType = "query"),
			@ApiImplicitParam(name = "pageNum", value = "起始行", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "Integer", paramType = "query")})
	@RequestMapping(value = "/getTeamInfoList", method = RequestMethod.GET)
	public String ManagerFindTeam(String teamType, String areaCode, String random,
								  Integer pageNum, Integer pageSize) {
		String result = null;
		Map<String, Object> params = new HashMap<>();
		params.put("teamType", teamType);
		params.put("areaCode", areaCode);
		params.put("random", random);
		PageDTO page = new PageDTO();
		page.setPageNum(pageNum);
		page.setPageSize(pageSize);
		Page<ManagerFindTeamInfo> list = managerFindTeamInfoService.selectTeamFindProjectList(params, page);
		result = responseSuccess(list);
		return result;
	}

	/**
	 *  
	 *
	 * @Description：
	 * @Author：yuxin
	 * @Create Date：2017-03-06
	 * @param：
	 * @return：
	 */
	@ApiOperation(value = "项目经理-找分包班组-班组承包人详情", response = TeamFindProjectInfo.class)
	@RequestMapping(value = "getdetail/{userCode}/{uuid}/{publishId}", method = RequestMethod.GET)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "项目经理的userCode", dataType = "String", required = true, paramType = "path"),
			@ApiImplicitParam(name = "uuid", value = "项目经理的uuid", dataType = "String", required = true, paramType = "path"),
			@ApiImplicitParam(name = "publishId", value = "发布时的id", dataType = "Integer", required = true, paramType = "path")
	})
	public String getDetail(@PathVariable String userCode, @PathVariable String uuid, @PathVariable Integer publishId) {
		Map map = new HashMap();
		map.put("userCode", userCode);
		map.put("uuid", uuid);
		map.put("publishId", publishId);
		map.put("type", CommonConstant.MANAGER_COLLECT_TEAM);//4代表项目经理收藏班组
		TeamFindProjectInfo teamFindProjectInfo = managerFindTeamInfoService.selectById(map);
		String result = responseSuccess(teamFindProjectInfo);
		return result;
	}

	@ApiOperation(value = "项目经理收藏班组")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "项目经理的userCode", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "uuid", value = "uuid", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "publishId", value = "发布id", required = true, dataType = "Integer", paramType = "path")
	})
	@RequestMapping(value = "/collectTeam/{userCode}/{uuid}/{publishId}", method = RequestMethod.POST)
	public String collectTeam(@PathVariable String userCode, @PathVariable String uuid, @PathVariable Integer publishId) {
		String result = null;
		Map map = new HashMap();
		map.put("userCode", userCode);
		map.put("uuid", uuid);
		map.put("publishId", publishId);
		map.put("type", CommonConstant.MANAGER_COLLECT_TEAM);//4代表项目经理收藏班组
		map.put("ctime", System.currentTimeMillis());
		int num = managerFindTeamInfoService.managerCollectionTeam(map);
		if (num > 0) {
			result = responseSuccess(num);
		} else {
			result = responseFail();
		}
		return result;
	}


	@ApiOperation(value = "项目经理取消收藏班组")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "项目经理的userCode", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "uuid", value = "uuid", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "publishId", value = "发布id", required = true, dataType = "Integer", paramType = "path")
	})
	@RequestMapping(value = "/cancelCollectTeam/{userCode}/{uuid}/{publishId}", method = RequestMethod.PUT)
	public String cancelCollectTeam(@PathVariable String userCode, @PathVariable String uuid, @PathVariable Integer publishId) {
		String result = null;
		Map map = new HashMap();
		map.put("userCode", userCode);
		map.put("uuid", uuid);
		map.put("publishId", publishId);
		map.put("type", CommonConstant.MANAGER_COLLECT_TEAM);//4代表项目经理收藏班组
		map.put("utime", System.currentTimeMillis());
		int num = managerFindTeamInfoService.cancelCollectionTeam(map);
		if (num > 0) {
			result = responseSuccess(num);
		} else {
			result = responseFail();
		}
		return result;
	}

	@ApiOperation(value = "项目经理收藏班组列表", response = ManagerFindTeamInfo.class)
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userCode", value = "项目经理的userCode", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "uuid", value = "uuid", required = true, dataType = "String", paramType = "path"),
			@ApiImplicitParam(name = "pageNum", value = "起始行", required = true, dataType = "Integer", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "一页显示的行数", required = true, dataType = "Integer", paramType = "query")
	})
	@RequestMapping(value = "/collectTeamList/{userCode}/{uuid}", method = RequestMethod.GET)
	public String collectTeamList(@PathVariable String userCode, @PathVariable String uuid,
								  @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
		String result = null;
		Map map = new HashMap();
		map.put("userCode", userCode);
		map.put("uuid", uuid);
		map.put("type", CommonConstant.MANAGER_COLLECT_TEAM);//4代表项目经理收藏班组
		map.put("pageNum", pageNum);
		map.put("pageSize", pageSize);
		Page<ManagerFindTeamInfo> page = managerFindTeamInfoService.getCollectionList(map);
		result = responseSuccess(page);
		return result;
	}
}
